<?php

/**
 * DEMO
 * 
 * require_once('../../_other/MQVCode.php');
 * $image = new MQVCode('80','20','6');    //图片长度、宽度、字符个数
 * $image->outImg();输出图片
 * $image->code;获得生成的验证码字符串
 *
 */


class Vcode extends LibBase{

        private $width=86;
        private $height=27;
        private $codenum=4;

        public $code;     //产生的验证码
        private $disturbColor = ''; //干扰像素

        private $image;    //验证码图片

        public function __construct(){
        }

        public function set($width,$height,$codenum){
                $this->width = $width;
                $this->height = $height;
                $this->codenum = $codenum;
        }


        public function outImg(){
           //输出头

           header ('Content-type: image/png');

           
           //产生验证码
           $this->createCode();

           //产生图片
           $this->createImage();
           //设置干扰像素
           $this->setDisturbColor();
           //往图片上写验证码
           $this->writeCodeToImage();
           $this->drawArcToImage();

           imagepng($this->image);
           imagedestroy($this->image);
			exit;
        }


        private function createCode(){

           $mdcode = strtoupper(md5(rand()));
           $mdcode = str_replace('0','R',$mdcode);
           $mdcode = str_replace('O','N',$mdcode);
           $this->code = substr($mdcode,0,$this->codenum);
           $_SESSION['vcode'] = $this->code;
        }


        private function createImage(){

           $this->image = imagecreate($this->width,$this->height);
           $back = imagecolorallocate($this->image,255,255,255);
           $border = imagecolorallocate($this->image,146,142,236);  
           imagefilledrectangle($this->image,1,1,$this->width - 1,$this->height - 1,$back); // 白色底
//         imagerectangle($this->image,0,0,$this->width - 1,$this->height - 1,$border);   // 黑色边框
        }

        private function setDisturbColor()
        {
        $this->disturbColor = imagecolorallocate ($this->image, 220,240,251);
//              $this->disturbColor = imagecolorallocate($this->image, rand(0,255), rand(0,255), rand(0,255));
           for ($i=0;$i<=2000;$i++)
           {
                imagesetpixel($this->image,rand(1,$this->width-1),rand(1,$this->height-1),$this->disturbColor);
           }
        }

        private function writeCodeToImage(){

           for ($i=0;$i<$this->codenum;$i++)
           {
        $bg_color = imagecolorallocate ($this->image, rand(0,255), rand(0,128), rand(0,255));
                $x = floor($this->width/$this->codenum)*$i+5;
                $y = rand(0,$this->height-15);
                imagechar ($this->image, rand(5,8), $x, $y, $this->code[$i], $bg_color);
           }
        
        }

        private function drawArcToImage(){
           for ($i=0;$i<3;$i++)
           {
                $bg_color = imagecolorallocate ($this->image, rand(0,255), rand(0,128), rand(0,255));
        imagearc($this->image, rand(0,$this->width), rand(0,$this->height), 30, 30, rand(3,90), rand(90,180), $bg_color);
        imagearc($this->image, rand(0,$this->width), rand(0,$this->height), 30, 30, rand(180,270), rand(270,360), $bg_color); 
           }
        }

        function __destruct(){

           unset($this->width,$this->height,$this->codenum);
        }
}